<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 订单接口
 */
class Order extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 我的订单
     * @param string $page 页码，第几页，默认为1
     * @param string $page_size 每页数量，默认为15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturnParams   (name="name", type="object", description="收货人姓名")
     * @ApiReturnParams   (name="mobile", type="object", description="手机号")
     * @ApiReturnParams   (name="area", type="object", description="地址")
     * @ApiReturnParams   (name="type", type="object", description="类型:1=默认,2=非默认")
     * @ApiReturnParams   (name="address", type="object", description="详细地址")
     *
     */
    public function getUserOrderList()
    {
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        
        $where = array(
            "user_id" => $this->auth->id
        );
        $result['list'] = DB::name("user_order")->where($where)->order("id desc")->page($page,$page_size)->select();
        $result['total'] = DB::name("user_order")->where($where)->count();
        $this->success('请求成功',$result);
    }
    
    
    /**
     * 申请发货
     *
     * @ApiMethod (POST)
     * @param string $user_bag_id  背包id
     * @param string $user_address_id  地址id
     * @param string $status 发货状态：1=无需发货，2=需要发货
     */
    public function doAddUserOrder()
    {
        $user_bag_id = $this->request->post('user_bag_id');
        $user_address_id = $this->request->post('user_address_id');
        $status = $this->request->post('status')?$this->request->post('status'):1;
        if (empty($user_bag_id)) {
            $this->error("请选择背包商品");
        }
        
        $user_bag = DB::name("user_bag")->where(array("id"=>$user_bag_id,"user_id"=>$this->auth->id))->find();
        if(empty($user_bag)){
            $this->error("背包商品有误");
        }
        if($user_bag['send_status'] != 1){
            $this->error('背包商品有误');
        }
        
        $system_prop = DB::name("system_prop")->where(array("id"=>$user_bag['system_prop_id']))->find();
        if($status == 1){
            DB::name("user_bag")->where(array("id"=>$user_bag_id,"user_id"=>$this->auth->id))->update(array("send_status"=>2));
            $this->success("拒绝发货成功");
        }else{
            if (empty($user_address_id)) {
                $this->error("请选择收货地址");
            }
            $user_address = DB::name("user_address")->where(array("id"=>$user_address_id,"user_id"=>$this->auth->id))->find();
            if(empty($user_address)){
                $this->error("收货地址有误");
            }
            DB::name("user_bag")->where(array("id"=>$user_bag_id,"user_id"=>$this->auth->id))->update(array("send_status"=>3));
            $data = array(
                "user_id" => $user_bag["user_id"],
                "user_bag_id" => $user_bag_id,
                "order_no" => date("YmdHis").rand(10000,99999),
                "name" => $system_prop['name'],
                "image" => $system_prop['image'],
                "money" => $system_prop['money'],
                "blurb" => $system_prop['blurb'],
                "address_username" => $user_address['name'],
                "address_mobile" => $user_address['mobile'],
                "address_detail" => $user_address['area'].$user_address['address'],
                "num" => 1,
                "status" => 1,
                "create_time" => date("Y-m-d H:i:s"),
            );
            // DB::name("user_order")->insert($data);
            $order_id = DB::name("user_order")->insertGetId($data);
            $result['order_id'] = $order_id;
            $this->success("申请发货成功",$result);
        }
        // $this->success("申请发货成功");
    }


    
    /**
     * 订单详情
     *
     * @param string $user_order_id  订单id
     */
    public function getUserOrderDetail()
    {
        $user_order_id = $this->request->post('user_order_id');
        if (empty($user_order_id)) {
            $this->error("请选择订单");
        }
        $user_order = DB::name("user_order")->where(array("id"=>$user_order_id,"user_id"=>$this->auth->id))->find();
        if(empty($user_order)){
            $this->error("订单有误");
        }
        $this->success("查询成功",$user_order);
    }
    
    
    /**
     * 确认收货
     * @ApiMethod (POST)
     * @param string $user_order_id  订单id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     *
     */
    public function editUserOrder()
    {
        $user_order_id = input("user_order_id")?input("user_order_id"):$this->error("订单id不能为空");
        if (empty($user_order_id)) {
            $this->error("请选择订单");
        }
        $user_order = DB::name("user_order")->where(array("id"=>$user_order_id,"user_id"=>$this->auth->id))->find();
        if(empty($user_order)){
            $this->error("订单有误");
        }
        if($user_order['status'] != 2){
            $this->error("订单状态有误！");
        }
        DB::name("user_order")->where(array("id"=>$user_order_id,"user_id"=>$this->auth->id))->update(array("status"=>3,"shou_time"=>date("Y-m-d H:i:s")));
        $this->success('确认收货成功');
    }
    
}
